package com.dlc.shop.user.platform.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.bean.param.UserUpdateParam;
import com.dlc.shop.common.allinpay.constant.PaySysType;
import com.dlc.shop.common.annotation.SysLog;
import com.dlc.shop.common.bean.PaySettlementConfig;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.service.SysConfigService;
import com.dlc.shop.user.common.model.UserBalance;
import com.dlc.shop.user.common.model.UserBalanceLog;
import com.dlc.shop.user.common.service.UserBalanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 * 余额充值级别表
 *
 * @author YXF
 * @date 2020-09-08 10:42:39
 */
@Tag(name = "余额充值等级")
@RestController
@AllArgsConstructor
@RequestMapping("/user/userBalance" )
public class UserBalanceController {

    private final SysConfigService sysConfigService;
    private final UserBalanceService userBalanceService;

    @SysLog("查看余额充值级别表" )
    @GetMapping("/getList" )
    @Operation(summary = "分页查询" , description = "分页查询")
    @PreAuthorize("@pms.hasPermission('user:userBalance:getList')" )
    public ServerResponseEntity<List<UserBalance>> getList(UserBalance userBalance) {
        return ServerResponseEntity.success(userBalanceService.getBalanceList());
    }

    @GetMapping("/info/{balanceId}" )
    @Operation(summary = "查询余额充值信息" , description = "通过id查询余额充值级别表")
    @Parameter(name = "balanceId", description = "余额充值id" )
    @PreAuthorize("@pms.hasPermission('user:userBalance:info')")
    public ServerResponseEntity<UserBalance> getById(@PathVariable("balanceId") Long balanceId) {
        return ServerResponseEntity.success(userBalanceService.getBalanceInfo(balanceId));
    }

    @SysLog("新增余额充值级别表" )
    @PostMapping
    @Operation(summary = "新增余额充值" , description = "新增余额充值")
    @PreAuthorize("@pms.hasPermission('user:userBalance:save')" )
    public ServerResponseEntity<String> save(@RequestBody @Valid UserBalance userBalance) {
        userBalanceService.saveBalance(userBalance);
        return ServerResponseEntity.success(userBalance.getBalanceId()+"");
    }

    @SysLog("修改余额充值级别表" )
    @PutMapping
    @Operation(summary = "修改余额充值" , description = "修改余额充值")
    @PreAuthorize("@pms.hasPermission('user:userBalance:update')" )
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid UserBalance userBalance) {
        userBalanceService.updateBalanceById(userBalance);
        userBalanceService.removeCacheByBalanceId();
        return ServerResponseEntity.success();
    }

    @SysLog("删除余额充值级别表" )
    @DeleteMapping("/{balanceId}" )
    @Operation(summary = "删除余额充值" , description = "删除余额充值")
    @PreAuthorize("@pms.hasPermission('user:userBalance:delete')" )
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long balanceId) {
        userBalanceService.removeById(balanceId);
        userBalanceService.removeCacheByBalanceId();
        return ServerResponseEntity.success();
    }

    @PutMapping("/updateUserBalance")
    @Operation(summary = "平台批量修改会员余额" , description = "平台批量修改会员余额")
    @PreAuthorize("@pms.hasPermission('platform:user:updateBalance')" )
    public ServerResponseEntity<Boolean> batchUpdateUserBalance(@RequestBody UserUpdateParam param) {
        PaySettlementConfig config = sysConfigService.getSysConfigObject(Constant.PAY_SETTLEMENT_CONFIG, PaySettlementConfig.class);
        if (Objects.equals(config.getPaySettlementType(), PaySysType.ALLINPAY.value())) {
            // 开启通联支付后，禁止余额修改
            throw new YamiShopBindException("yami.allinpay.balance.update.forbid");
        }
        return ServerResponseEntity.success(userBalanceService.batchUpdateUserBalance(param));
    }

    @GetMapping("/getPageByUserId" )
    @Operation(summary = "分页查询指定用户的余额明细" , description = "分页查询指定用户的余额明细")
    @Parameter(name = "userId", description = "用户id" )
    @PreAuthorize("@pms.hasPermission('user:userBalance:pageLog')")
    public ServerResponseEntity<IPage<UserBalanceLog>> getPageByUserId(PageParam<UserBalanceLog> page, String userId) {
        PaySettlementConfig config = sysConfigService.getSysConfigObject(Constant.PAY_SETTLEMENT_CONFIG, PaySettlementConfig.class);
        IPage<UserBalanceLog> res = userBalanceService.getPageByUserId(page, userId, config.getPaySettlementType());
        return ServerResponseEntity.success(res);
    }
}
